var SdsOrder = {
    SdsOrderData: {},
    layerIndex: -1
};

/**
 * 获取对话框中的数据
 *
 * param key 数据的名称
 * param val 数据的具体值
 */
SdsOrder.get = function (key, type) {

    var res = "";
    switch (type) {
        case "checkbox":
            $("input[type='checkbox'][name=" + key + "]:checked").each(function () {
                //  选中复选框中的值
                res += $(this).val() + ",";
            });
            res = res.substring(0, res.length - 1)
            break;
        case "radio":
            var obj = $("input[type='radio'][name=" + key + "]:checked");
            res = obj.val();
            break;
        default:
            res = $("input[name=" + key + "]").val();
            break;
    }
    return res;
};

/**
 * 收集数据
 */
SdsOrder.collectData = function () {
    $(".item").each(function () {
        var key = $(this)[0].id;
        key = key.split("_")[0];    // 为了解决两个禁忌症前置项同为一个项目，单选按钮互斥效果紊乱

        if (SdsOrder.SdsOrderData[key] != "y") {
            SdsOrder.SdsOrderData[key] = SdsOrder.get($(this)[0].id, $(this).attr("ciType"));
        }
    });
};

/**
 * 医嘱一键录入按钮
 */
SdsOrder.orderToDoc = function () {
    var orderIds = "";
    $('input[name="clcAssisBox"]:checked').each(function () {
        orderIds += $(this).val() + "#";
    });
    if (orderIds == "") {
        layer.msg("请勾选医嘱", {icon: 3});
    } else {
        var ajax = new $ax("/api/sdstool/addOrderToHis", function (data) {
            if (data == 0) {
                layer.msg("插入医嘱成功,请打开医嘱录入界面审核!", {icon: 1});
                parent.postMessage("save", "*");
                // SdsOrder.updateEventRecord();
            } else {
                layer.msg("插入医嘱失败!", {icon: 2});
            }
        }, function (data) {
            layer.msg("插入医嘱失败!", {icon: 2});
        });
        var params = {};
        params.orderIds = orderIds;
        params.EpisodeID = $("#episodeId").val(); // 临床就诊号
        params.PatientID = $("#patientId").val(); // 数据中心就诊ID
        ajax.setData(params);
        ajax.start();
    }
    return false;
};

/**
 * 更新事件记录
 */
SdsOrder.updateEventRecord = function () {
    var ajax = new $ax("/api/sdstool/updateEventRecord");
    var params = {};
    params.PatientID = $("#patientId").val(); // 数据中心就诊ID
    params.recordId = $("#recordId").val();
    params.eventId = $("#eventId").val();
    ajax.setData(params);
    ajax.start();
}

/**
 * 关闭弹窗
 */
SdsOrder.close = function () {
    parent.postMessage("close", "*");
};

$(function () {
    var ordStr = "";
    // 获取阶段信息
    var ajax = new $ax("/api/sdstool/getOrderInfo", function (data) {
        var l = data.length;
        if (l == 0) {
            ordStr = "暂未维护关联医嘱项目";
        }
        for (let i = 0; i < l; i++) {
            var item = data[i];
            var itemName = item.itemName;
            var itemList = item.itemList;
            ordStr += "<div name=\"itemTitle\" style='font-weight: bold; background-color: #B8D6E2'>"+itemName+"</div>";
            ordStr += "<div name=\"content\" style='background-color: '>";
            var m = itemList.length;
            if (m==0) {
                ordStr += "<div style='color: grey'>未关联医嘱项</div>";
            } else {
                for (let j = 0; j < m; j++) {
                    var ord = itemList[j];
                    var checkFlag = (ord.isActive == 1 ? "checked=\"true\" " : "");
                    ordStr += "<div><input class=\"hisui-checkbox\" type=\"checkbox\" name=\"clcAssisBox\" " + checkFlag +
                        "id=\"" + ord.xcode + "\" label=\"" + ord.description + "\" value=\"" + ord.xcode + "\" ></div>\n";
                }
            }
            ordStr += "</div>";
        }
        $("#orderDiv").append(ordStr);
        $.parser.parse("#orderDiv");
    });
    var params = {};
    params.itemId = $('#itemId').val();
    params.entityId = $('#entityId').val();
    params.planType = $('#planType').val();
    ajax.set(params);
    ajax.start();
});